package com.github.aloxc.plugin.restplus.component;

import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.common.Consts;
import com.intellij.ui.components.JBPanel;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * 导入postman数据的窗体
 * @author leero
 */
public class Postman extends JBPanel {
    private JPanel rootPanel;
    public JTextField fileAddress;
    public JPanel postmanPanel;
    public JButton fileButton;
    private JLabel chooseLabel;
    final JProgressBar progressBar;
    private JFileChooser fileChooser = new JFileChooser();

    @Override
    protected void printComponent(Graphics g) {
        super.printComponent(g);
    }



    public Postman() {
        super();
        progressBar = new JProgressBar();
        initComponent();
    }

    private void initComponent() {
        initUi();
        initAction();
    }


    private void initAction() {
        fileButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FileNameExtensionFilter filter = new FileNameExtensionFilter("*.json", "JSON");
                fileChooser.setFileFilter(filter);
                int i = fileChooser.showOpenDialog(Postman.this);// 显示文件选择对话框
                // 判断用户单击的是否为“打开”按钮
                if (i == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();// 获得选中的文件对象
                    int result = JOptionPane.showConfirmDialog(
                            Postman.this,
                            I18Bundle.getMessage("com.github.aloxc.plugin.restplus.postman.tips.info"),
                            I18Bundle.getMessage("com.github.aloxc.plugin.restplus.postman.tips.title"),
                            JOptionPane.YES_NO_CANCEL_OPTION
                    );
                    progressBar.setString(null);
                    progressBar.setValue(0);
                    switch (result) {
                        case JOptionPane.YES_OPTION:
                            fileAddress.setText(selectedFile.getAbsolutePath());// 显示选中文件的名称
                            progressBar.setStringPainted(true);
                            progressBar.setForeground(Consts.MAIN_COLOR);
                            progressBar.setVisible(true);
                            int size = 100;
                            new Thread(){
                                @Override
                                public void run(){
                                    for(int i=0;i<=size;i++){
                                        try{
                                            Thread.sleep(100);
                                        }catch(InterruptedException e){
                                            e.printStackTrace();
                                        }
                                        progressBar.setValue(i);
                                    }
                                    progressBar.setString(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.postman.tips.finish"));
                                }
                            }.start();
                            break;
                        case JOptionPane.CANCEL_OPTION:
                        case JOptionPane.NO_OPTION:
                        default:
                            break;
                    }
                }
            }
        });
    }


    private void initUi() {
        chooseLabel.setText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.postman.info"));
        fileButton.setText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.postman.button.text"));
        fileAddress.setAutoscrolls(true);
        postmanPanel = new JBPanel();
        GridLayoutManager mgr = new GridLayoutManager(3, 3);
        mgr.setHGap(1);
        mgr.setVGap(0);
        mgr.setMargin(new Insets(20, 20, 20, 20));
        postmanPanel.setLayout(mgr);

        Dimension dimension = new Dimension(400, 28);
        progressBar.setStringPainted(true);
        progressBar.setForeground(Consts.MAIN_COLOR);
        progressBar.setBackground(Consts.MAIN_COLOR);
        progressBar.setPreferredSize(new Dimension(380,20));
        progressBar.setVisible(false);
        JPanel contentPane=new JPanel();
        contentPane.add(progressBar,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        dimension, dimension, dimension));
        postmanPanel.add(contentPane,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        dimension, dimension, dimension));
        dimension = new Dimension(-1, 28);
        postmanPanel.add(chooseLabel,
                new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        dimension, dimension, dimension));
        dimension = new Dimension(-1, 28);

        postmanPanel.add(fileAddress,
                new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        dimension, dimension, dimension));
        dimension = new Dimension(50, 28);
        postmanPanel.add(fileButton,
                new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED,
                        dimension, dimension, dimension));
        this.setBorder(BorderFactory.createEmptyBorder());
        this.setLayout(new GridLayoutManager(2, 1));
        this.add(postmanPanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        null, null, null));
    }
}
